package com.ruoyi.wapp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huobi.client.model.Trade;
import com.huobi.client.model.TradeStatistics;
import com.ruoyi.common.constant.CacheKeyConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.WdyDeal;
import com.ruoyi.system.domain.WdyKline;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.IWdyDealService;
import com.ruoyi.system.service.IWdyKlineService;
import com.ruoyi.system.service.IWdyTradeService;
import com.ruoyi.wapp.controller.base.BaseController;
import com.ruoyi.wapp.controller.base.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/trade")
public class TradeController extends BaseController {

    @Autowired
    private IWdyTradeService wdyTradeService;

    @Autowired
    private IWdyDealService wdyDealService;

    @Autowired
    private IWdyKlineService wdyKlineService;

    @Autowired
    private ISysConfigService sysConfigService;

    @GetMapping("/getMarketDepth")
    public Result getMarketDepth(String depth) {
        if(depth == null) {
            depth = "5";
        } else {
            depth = "20";
        }
        String buyMarketDepthKey = CacheKeyConstants.getTempletString(CacheKeyConstants.MARKET_DEPTH_BUY, depth);
        String sellMarketDepthKey = CacheKeyConstants.getTempletString(CacheKeyConstants.MARKET_DEPTH_SELL, depth);
        List<Map<String, Object>> buyList = (List<Map<String, Object>>) redisTemplate.opsForValue().get(buyMarketDepthKey);
        List<Map<String, Object>> sellList = (List<Map<String, Object>>) redisTemplate.opsForValue().get(sellMarketDepthKey);
        return Result.ok().put("buyList", buyList).put("sellList", sellList);
    }

    @GetMapping("/getDealList")
    public Result getDealList() {
        String dealListKey = CacheKeyConstants.getTempletString(CacheKeyConstants.DEALLIST, String.valueOf(60));
        List<WdyDeal> list = (List<WdyDeal>) redisTemplate.opsForValue().get(dealListKey);
        return Result.ok().put("list", list);
    }

    @GetMapping("/getKline")
    public Result getKline(Integer lineType, String startTime, String endTime) {
        if(lineType == null) {
            lineType = WdyKline.TYPE_DAY;
        }
        if(StringUtils.isEmpty(startTime)) {
            if(lineType == WdyKline.TYPE_DAY)
                startTime = DateUtils.getCurrentMonthFirstDay() + " 00:00:00";
            else
                startTime = DateUtils.getDate() + " 00:00:00";
        }
        if(StringUtils.isEmpty(endTime)) {
            if(lineType == WdyKline.TYPE_DAY)
                endTime = DateUtils.getCurrentMonthLastDayStr() + " 23:59:59";
            else
                endTime = DateUtils.getDate() + " 23:59:59";
        }
        QueryWrapper<WdyKline> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("stats_time", startTime);
        queryWrapper.le("stats_time", endTime);
        queryWrapper.eq("line_type", lineType);
        queryWrapper.orderByAsc("stats_time");
        List<WdyKline> list = wdyKlineService.list(queryWrapper);
        List<Object[]> data = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)) {
            for(WdyKline e : list) {
                data.add(getObjectArray(e, lineType == WdyKline.TYPE_DAY ? DateUtils.YYYY_MM_DD : DateUtils.YYYY_MM_DD_HH_MM_SS));
            }
        }
        WdyKline kline = wdyKlineService.calKline(new Date(), lineType);
        if(kline != null) {
            data.add(getObjectArray(kline, lineType == WdyKline.TYPE_DAY ? DateUtils.YYYY_MM_DD : DateUtils.YYYY_MM_DD_HH_MM_SS));
        }
        return Result.ok().put("data", data);
    }

    private Object[] getObjectArray(WdyKline e, String format) {
        return new Object[]{DateUtils.format(e.getStatsTime(),format),
                e.getOpen(), e.getClose(), e.getLow(), e.getHigh(), e.getVolumn(), 0, 0, 0, 0};
    }

    @GetMapping("/getLatestPrice")
    public Result getLatestPrice() {
        Map<String, Object> map = redisTemplate.opsForHash().entries(CacheKeyConstants.LATESTPRICE);
        Result result = Result.ok();
        result.putAll(map);
        return result;
    }

    @GetMapping("/huobi/get24HTradeStatistics")
    public Result huobiGet24HTradeStatistics(String symbol) {
        TradeStatistics data = (TradeStatistics) redisTemplate.opsForHash().get(CacheKeyConstants.HUOBI_TRADESTATISTICS_24H, symbol);
        return Result.ok().put("data", data);
    }

    @GetMapping("/huobi/getTradeDetail")
    public Result huobiGetTradeDetail(String symbol) {
        List<Trade> list = (List<Trade>) redisTemplate.opsForHash().get(CacheKeyConstants.HUOBI_TRADE_DETAIL, symbol);
        return Result.ok().put("list", list);
    }
}
